window.appPlug = window.appPlug || {};
(function (me, platform) {
  Object.assign(me, {
    platform: platform || 'android',

    /**
     *  调用插件发现，cordova 未初始完成时调用
     *  @param
     */
    notReady: function (fail) {
      fail && fail();
      console.log('codova not ready');
    },

    /**
     *  cordova 初始化完成后，注册方法
     *  @param array funArray 方法数组
     */
    onReady: function (funArray) {
      if (window.appPlugReady === true) {
        for (var i in funArray) {
          funArray[i]();
        }
      } else {
        funArray && (this._onReadyFunArray = this._onReadyFunArray.concat(funArray));
      }
    },

    init: function () {
      window.appPlugReady = false;

      onDeviceReady = function () {
        me._deviceReadyDefault();
        for (var i in me._onReadyFunArray) {
          // alert('_onReadyFunArray');
          // alert(JSON.stringify(me._onReadyFunArray));
          me._onReadyFunArray[i]();
        }
      }
      document.addEventListener("deviceready", onDeviceReady, false);
    },

    _onReadyFunArray: [],

    // 默认启动事件
    _deviceReadyDefault: function () {
      window.appPlugReady = true;
      // alert('_deviceReadyDefault');

      if (platform === 'android') {
        // 注册安卓键事件

        // 退出启动页，显示js
        // me.exitLancher();

        // 需要在业务模块重写这个方法
        // window.appPlug.onBackKey = function () {};
        // 返回键
        // window.TeacherUtil.setBackKeyCallBack("window.appPlug.onBackKey");
      }
    },

    onBackKey: function () {

    },

    /**
     * 下载作业包
     * @param function succ 
     * @param function fail 
     * @param data [{
            "url": "https://static.anoah.com/uploads/ebag_data/apk/126/29/screenrecord2_191101.apk", //下载链接
            "publishId": "1GhFzR2Yxr61g0oigrT41L", //发布id
            "pkgId": "0", //作业包id
            "userId": "123", //用户id
            "action": "download/cancel" //download:下载作业包。cacel:取消作业包下载（暂停=取消）
          }]
     */

    HpkDownload: function (data, succ, fail) {
      if (!window.appPlugReady && window.appPlug.notReady(fail)) {
        return;
      }
      if (platform === 'android') {
        cordova.exec(function (r) {
          succ && succ(r);
          },
          function (r) {
            fail && fail(r);
          },
          "HpkDownloadPlugin", "download", data);
      } else if (platform === 'ios') {
        // cordova.exec(null, null, "HaleyPlugin", "startNewHtmlWithURL", [href]);
      }
    },
    // 上传base64数据
    uploadBase64: function (data, succ, fail) {
      if (!window.appPlugReady && window.appPlug.notReady(fail)) {
        return;
      }
      if (platform === 'android') {
        cordova.exec(function (r) {
              succ && succ(r);
            },
            function (r) {
              fail && fail(r);
            },
            "Base64ToImgPlugin", "base64ToImg", data);
      } else if (platform === 'ios') {
        // cordova.exec(null, null, "HaleyPlugin", "startNewHtmlWithURL", [href]);
      }
    },

    loadUrl: function(url){
      if (!window.appPlugReady && window.appPlug.notReady()) {
        return;
      }
      if (platform === 'android') {
        window.web_interface && window.web_interface.loadUrl &&  window.web_interface.loadUrl(url);
      } else if (platform === 'ios') {
      }
    },

    getParams: function() {
      var r = '';
      if (!window.appPlugReady && window.appPlug.notReady()) {
        console.log('getParams return');
        return;
      }
      console.log('getParams platform', platform);
      if (platform === 'android') {
        window.web_interface && window.web_interface.getConfig && (r = window.web_interface.getConfig());
        console.log('getParams r', r);
      } else if (platform === 'ios') {
      }
      return r
    }

  });
  me.init();
})(window.appPlug, window.platform)